# Replication Archive for 
# Alexander Coppock, Emily Ekins and David Kirby (2018), 
# "The Long-lasting Effects of Newspaper Op-Eds on Public Opinion",
# Quarterly Journal of Political Science: Vol. 13: No. 1, pp 59-87.

rm(list = ls())

# Uncomment to install
# install.packages(c("tidyverse", "stargazer", "estimatr", "xtable"))

library(tidyverse)
library(stargazer)
library(estimatr)
library(xtable)

# Set working directory to replication archive

load("mturk_opeds_cleaned.rdata")
load("elite_opeds_cleaned.rdata")
load("dv_df.rdata")
source("opeds_source.R")

# Missingness not related to treatment ------------------------------------

fit_1 <- lm(responded_w2 ~ Z, data = elite_opeds)
fit_2 <- lm(responded_w2 ~ Z, data = mturk_opeds)
fit_3 <- lm(responded_w3 ~ Z, data = mturk_opeds)

stargazer(
  fit_1,
  fit_2,
  fit_3,
  se = starprep(fit_1, fit_2, fit_3),
  p = starprep(fit_1, fit_2, fit_3, stat = "p.value"),
  style = "apsr",
  column.sep.width = "0pt",
  digits = 3,
  omit.stat = c("adj.rsq", "f", "ser"),
  add.lines = list(c("Sample", "Elite", "MTurk", "MTurk")),
  dep.var.labels = c("Responded Wave 2", "Responded Wave 3"),
  covariate.labels = c(
    "Op-ed: Amtrak",
    "Op-ed: Climate",
    "Op-ed: Flat Tax",
    "Op-ed: Veterans",
    "Op-ed: Wall Street",
    "Constant (Constant)"
  ),
  model.numbers = FALSE,
  notes = c("Models estimated via OLS. Robust standard errors in parentheses."),
  notes.append = TRUE,
  font.size = "small",
  float = TRUE,
  align = TRUE,
  label = "tab:attrition",
  title = "Predicting Follow-up Response From Treatment Condition"
)

# Missingness related to covariates ---------------------------------------
elite_opeds <- 
  mutate(elite_opeds, pid_7_c = pid_7 - 4)
mturk_opeds <-
  mutate(mturk_opeds, pid_7_c = pid_7 - 4)

fit_1 <- glm(responded_w2 ~ age_5 + educ_5 + race_4 + ideo_5 + female + pid_7_c, family = "binomial", data = elite_opeds)
fit_2 <- glm(responded_w2 ~ age_5 + educ_5 + race_4 + ideo_5 + female + pid_7_c, family = "binomial", data = mturk_opeds)
fit_3 <- glm(responded_w3 ~ age_5 + educ_5 + race_4 + ideo_5 + female + pid_7_c, family = "binomial", data = mturk_opeds)

stargazer(
  fit_1,
  fit_2,
  fit_3,
  style = "apsr",
  column.sep.width = "0pt",
  digits = 3,
  omit.stat = c("adj.rsq", "f", "ser"),
  add.lines = list(c("Sample", "Elite", "MTurk", "MTurk")),
  dep.var.labels = c("Responded Wave 2", "Responded Wave 3"),
  covariate.labels = c(
    "Age: 30 - 39",
    "Age: 40 - 49",
    "Age: 50 - 59",
    "Age: 60+",
    "High School",
    "Some College",
    "College",
    "Graduate School",
    "Hispanic",
    "White",
    "Other Race",
    "Moderate",
    "Libertarian",
    "Conservative",
    "Other",
    "Female",
    "7-point Party ID",
    "Constant"
  ),
  model.numbers = FALSE,
  notes = c(
    "Models estimated via logistic regression.",
    "Omitted Categories: Age 18-29, Less than High School, Black, Liberal, Male"
  ),
  notes.append = TRUE,
  font.size = "small",
  float = TRUE,
  align = TRUE,
  label = "tab:attrition_demos",
  title = "Predicting Follow-up Response From Demographics"
)


# Making Demographic Tables by Wave ---------------------------------------

mturk_long <-
  mturk_opeds %>%
  gather(wave, responded, responded_w1, responded_w2, responded_w3) %>%
  filter(responded)

age_mturk <-
  mturk_long %$%
  table(age_5, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c("18 - 29", "30 - 39", "40 - 49", "50 - 59", "60+"))) %>%
  arrange(rowname)

educ_mturk <- 
  mturk_long %$%
  table(educ_5, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c("Less than High School", "High School", "Some College", "College", "Graduate School"))) %>%
  arrange(rowname)

race_mturk <- 
  mturk_long %$%
  table(race_4, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c("Black", "Hispanic", "White", "Other"))) %>%
  arrange(rowname)

pid_mturk <- 
  mturk_long %$%
  table(pid_7, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = 1:7,
                          labels = c("Strong Democrat",
                                     "Not very strong Democrat",
                                     "Lean Democrat",
                                     "Independent",
                                     "Lean Republican",
                                     "Not very strong Republican",
                                     "Strong Republican"))) %>%
  arrange(rowname)

ideo_mturk <- 
  mturk_long %$%
  table(ideo_5, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c("Liberal", "Moderate", "Libertarian", "Conservative", "Other"))) %>%
  arrange(rowname)

female_mturk <- 
  mturk_long %$%
  table(female, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c(0, 1), labels = c("Male", "Female"))) %>%
  arrange(rowname)

n_mturk <- 
  mturk_long %$% 
  table(wave) %>%
  as.matrix(.) %>% t %>%
  data.frame() %>%
  rownames_to_column() %>%
  mutate(rowname = "N")


xtable(age_mturk, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(educ_mturk, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(race_mturk, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(pid_mturk, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(ideo_mturk, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(female_mturk, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(n_mturk) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())



elite_long <- 
  elite_opeds %>% 
  gather(wave, responded, responded_w1, responded_w2) %>%
  filter(responded)


age_elite <- 
  elite_long %$%
  table(age_5, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c("18 - 29", "30 - 39", "40 - 49", "50 - 59", "60+"))) %>%
  arrange(rowname)

educ_elite <- 
  elite_long %$%
  table(educ_5, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c("Less than High School", "High School", "Some College", "College", "Graduate School"))) %>%
  arrange(rowname)

race_elite <- 
  elite_long %$%
  table(race_4, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c("Black", "Hispanic", "White", "Other"))) %>%
  arrange(rowname)

pid_elite <- 
  elite_long %$%
  table(pid_7, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = 1:7,
                          labels = c("Strong Democrat",
                                     "Not very strong Democrat",
                                     "Lean Democrat",
                                     "Independent",
                                     "Lean Republican",
                                     "Not very strong Republican",
                                     "Strong Republican"))) %>%
  arrange(rowname)


ideo_elite <- 
  elite_long %$%
  table(ideo_5, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c("Liberal", "Moderate", "Libertarian", "Conservative", "Other"))) %>%
  arrange(rowname)

female_elite <- 
  elite_long %$%
  table(female, wave) %>%
  prop.table(., margin = 2) %>%
  as.data.frame.matrix(.) %>%
  rownames_to_column() %>%
  mutate(rowname = factor(rowname, levels = c(0, 1), labels = c("Male", "Female"))) %>%
  arrange(rowname)


n_elite <- 
  elite_long %$% 
  table(wave) %>%
  as.matrix(.) %>% t %>%
  data.frame() %>%
  rownames_to_column() %>%
  mutate(rowname = "N")

xtable(age_elite, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(educ_elite, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(race_elite, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(pid_elite, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(ideo_elite, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(female_elite, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())
xtable(n_elite, digits = 3) %>% print.xtable(include.rownames = FALSE, include.colnames = FALSE, only.contents = TRUE, hline.after = c())

